package com.xb.cloud.usercenter.client;

import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import com.xb.cloud.common.JsonUtils;
import com.xb.cloud.gateway.client.Adapter;
import com.xb.cloud.gateway.client.AdapterContext;
import com.xb.cloud.gateway.client.RtnInfo;
import com.xb.cloud.user.service.client.bo.EndUserAddressBo;
import com.xb.cloud.usercenter.service.UserAddressApiService;

/**
 * 
 * @ClassName: UserAddressAdapter
 * @Description:用户地址管理
 * @author: 鲜备科技
 * @date: 2018年1月12日 下午4:14:06
 * 
 * @Copyright: 2018 www.xianbeitech.com Inc. All rights reserved.
 *
 */
@Component
public class UserAddressAdapter extends Adapter {

	private final Logger logger = LoggerFactory.getLogger(UserAddressAdapter.class);

	@Resource
	UserAddressApiService userAddressApiService;

	/**
	 * 
	 * <p>
	 * Title: execute
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @param urlPath
	 * @param isLogined
	 * @param bizInfo
	 * @param context
	 * @return
	 * @see com.xb.cloud.gateway.client.Adapter#execute(java.lang.String,
	 *      java.lang.Boolean, java.util.Map,
	 *      com.xb.cloud.gateway.client.AdapterContext)
	 */
	public RtnInfo execute(String urlPath, Boolean isLogined, Map<String, String> bizInfo, AdapterContext context) {
		long time = System.currentTimeMillis();

		RtnInfo rtnInfo = null;
		long companyId = context.getCompanyId();
		long currentUserId = context.getCurrentUserId();

		if ("/h5/userAddress/create".equalsIgnoreCase(urlPath)) {
			String item = bizInfo.get("item");
			EndUserAddressBo endUserAddressBo = JsonUtils.jsonToBean(item, EndUserAddressBo.class);
			endUserAddressBo.setCompanyId(companyId);
			endUserAddressBo.setEndUserId(currentUserId);
			return userAddressApiService.createtUserAddress(endUserAddressBo);
		}

		if ("/h5/userAddress/list".equalsIgnoreCase(urlPath)) {
			return userAddressApiService.getUserAddressList(companyId, currentUserId);
		}

		if ("/h5/userAddress/update".equalsIgnoreCase(urlPath)) {
			String item = bizInfo.get("item");
			EndUserAddressBo endUserAddressBo = JsonUtils.jsonToBean(item, EndUserAddressBo.class);
			endUserAddressBo.setCompanyId(companyId);
			endUserAddressBo.setEndUserId(currentUserId);
			return userAddressApiService.updateUserAddressById(endUserAddressBo);
		}

		if ("/h5/userAddress/delete".equalsIgnoreCase(urlPath)) {
			Long id = Long.valueOf(bizInfo.get("id"));
			return userAddressApiService.deleteUserAddressById(id);
		}

		if ("/h5/userAddress/getProvinces".equalsIgnoreCase(urlPath)) {
			return userAddressApiService.getProvinces(companyId);
		}

		if ("/h5/userAddress/getCitys".equalsIgnoreCase(urlPath)) {
			Long provinceId = Long.valueOf(bizInfo.get("provinceId"));
			return userAddressApiService.getCitys(provinceId);
		}

		if ("/h5/userAddress/getCountys".equalsIgnoreCase(urlPath)) {
			Long cityId = Long.valueOf(bizInfo.get("cityId"));
			return userAddressApiService.getCountys(cityId);
		}

		if ("/h5/userAddress/getAreas".equalsIgnoreCase(urlPath)) {
			Long countyId = Long.valueOf(bizInfo.get("countyId"));
			return userAddressApiService.getAreas(countyId);
		}

		logger.debug("执行时间：{}", System.currentTimeMillis() - time);
		return rtnInfo;
	}

	@Override
	public String getUrlMappingPath() {
		return "/h5/userAddress/**";
	}

	@Override
	public boolean isCheckUserToken() {
		return true;
	}

}
